    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 30/08/2009, 2:44:22 AM
 */

package net.animats.music.app;

import net.animats.music.*;
import javax.swing.JLabel;

/**
 *
 * @author stuart
 */
public class Kielbasa extends javax.swing.JFrame {

    GUISong song = new GUISong();

    public void removeLast() {
        if (song.removeLast() == 0) {
            songLabel.setText(" Empty Song");
            consoleArea.setText(null);
            delButton.setEnabled(false);
            transposeUpButton.setEnabled(false);
            transposeDownButton.setEnabled(false);
        } else {
            updateSongDisplay();
        }

    }

    private void updateSongDisplay() {
        songLabel.setText(song.getChordText());
        String consoleText = "Possible keys song is in:\n";
        for (Key key : Key.getKeys()) {
            // GET EACH OF THE KNOWN KEYS AND TEST THE SONG AGAINST IT
            if (key.inThisKey(song)) {
                consoleText += "   " + key.getName() + "\n";
            }
        }

        consoleArea.setText(consoleText);
    }

    /** Creates new form MainFrame */
    public Kielbasa() {
        initComponents();
        noteSelectorPanelChords.addListener((NoteSelectorListener) chordLabel);
        chordSelectorPanel.addListener(song);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainTabs = new javax.swing.JTabbedPane();
        songTab = new javax.swing.JPanel();
        consoleScrollPane = new javax.swing.JScrollPane();
        consoleArea = new javax.swing.JTextArea();
        chordSelectorPanel = new ChordSelectorPanel("Add");
        songLabel = new javax.swing.JLabel();
        delButton = new javax.swing.JButton();
        transpositionPanel = new javax.swing.JPanel();
        transposeLabel = new javax.swing.JLabel();
        transposeDownButton = new javax.swing.JButton();
        transposeUpButton = new javax.swing.JButton();
        chordsTab = new javax.swing.JPanel();
        noteSelectorPanelChords = new net.animats.music.app.NoteSelectorPanel();
        chordLabel = new IntevalSetLabel();
        scalesTab = new javax.swing.JPanel();
        noteSelectorPanelScales = new net.animats.music.app.NoteSelectorPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Kielbasa");

        mainTabs.setToolTipText("Create a song from a set of chords.");

        consoleArea.setBackground(new java.awt.Color(40, 40, 40));
        consoleArea.setColumns(20);
        consoleArea.setFont(new java.awt.Font("DejaVu Sans", 0, 18));
        consoleArea.setForeground(new java.awt.Color(0, 200, 200));
        consoleArea.setRows(5);
        consoleArea.setMaximumSize(new java.awt.Dimension(300, 200));
        consoleArea.setMinimumSize(new java.awt.Dimension(300, 200));
        consoleScrollPane.setViewportView(consoleArea);

        songLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 36));
        songLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        songLabel.setText(" Empty Song");
        songLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        songLabel.setMaximumSize(new java.awt.Dimension(1024, 50));
        songLabel.setMinimumSize(new java.awt.Dimension(250, 50));
        songLabel.setPreferredSize(new java.awt.Dimension(250, 50));

        delButton.setText("Delete");
        delButton.setToolTipText("Delete last chord added to the song.");
        delButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        delButton.setEnabled(false);
        delButton.setMaximumSize(new java.awt.Dimension(80, 50));
        delButton.setMinimumSize(new java.awt.Dimension(80, 50));
        delButton.setPreferredSize(new java.awt.Dimension(80, 50));
        delButton.setRolloverEnabled(false);
        delButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                delButtonMouseClicked(evt);
            }
        });

        transpositionPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        transpositionPanel.setMaximumSize(new java.awt.Dimension(32767, 55));
        transpositionPanel.setMinimumSize(new java.awt.Dimension(100, 55));

        transposeLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 18));
        transposeLabel.setText("Tranpose Song:");

        transposeDownButton.setText("Down");
        transposeDownButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        transposeDownButton.setEnabled(false);
        transposeDownButton.setMaximumSize(new java.awt.Dimension(160, 27));
        transposeDownButton.setMinimumSize(new java.awt.Dimension(120, 27));
        transposeDownButton.setPreferredSize(new java.awt.Dimension(130, 27));
        transposeDownButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                transposeDownButtonMouseClicked(evt);
            }
        });

        transposeUpButton.setText("Up");
        transposeUpButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        transposeUpButton.setEnabled(false);
        transposeUpButton.setMaximumSize(new java.awt.Dimension(160, 27));
        transposeUpButton.setMinimumSize(new java.awt.Dimension(120, 27));
        transposeUpButton.setPreferredSize(new java.awt.Dimension(130, 27));
        transposeUpButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                transposeUpButtonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout transpositionPanelLayout = new javax.swing.GroupLayout(transpositionPanel);
        transpositionPanel.setLayout(transpositionPanelLayout);
        transpositionPanelLayout.setHorizontalGroup(
            transpositionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, transpositionPanelLayout.createSequentialGroup()
                .addContainerGap(50, Short.MAX_VALUE)
                .addComponent(transposeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(transposeDownButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(transposeUpButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        transpositionPanelLayout.setVerticalGroup(
            transpositionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(transpositionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(transpositionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(transposeUpButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(transposeDownButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(transposeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout songTabLayout = new javax.swing.GroupLayout(songTab);
        songTab.setLayout(songTabLayout);
        songTabLayout.setHorizontalGroup(
            songTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(songTabLayout.createSequentialGroup()
                .addGroup(songTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, songTabLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(songLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 728, Short.MAX_VALUE)
                        .addGap(6, 6, 6)
                        .addComponent(delButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(songTabLayout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(chordSelectorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(transpositionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(songTabLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(consoleScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 814, Short.MAX_VALUE)))
                .addContainerGap())
        );
        songTabLayout.setVerticalGroup(
            songTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(songTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(songTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(songLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(delButton, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(songTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(transpositionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chordSelectorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(consoleScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                .addContainerGap())
        );

        mainTabs.addTab("Song", songTab);

        javax.swing.GroupLayout noteSelectorPanelChordsLayout = new javax.swing.GroupLayout(noteSelectorPanelChords);
        noteSelectorPanelChords.setLayout(noteSelectorPanelChordsLayout);
        noteSelectorPanelChordsLayout.setHorizontalGroup(
            noteSelectorPanelChordsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 421, Short.MAX_VALUE)
        );
        noteSelectorPanelChordsLayout.setVerticalGroup(
            noteSelectorPanelChordsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 121, Short.MAX_VALUE)
        );

        chordLabel.setFont(new java.awt.Font("DejaVu Sans", 0, 36));
        chordLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout chordsTabLayout = new javax.swing.GroupLayout(chordsTab);
        chordsTab.setLayout(chordsTabLayout);
        chordsTabLayout.setHorizontalGroup(
            chordsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chordsTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(chordsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chordLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                    .addComponent(noteSelectorPanelChords, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(405, Short.MAX_VALUE))
        );
        chordsTabLayout.setVerticalGroup(
            chordsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chordsTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(noteSelectorPanelChords, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(275, Short.MAX_VALUE))
        );

        mainTabs.addTab("Chords", chordsTab);

        javax.swing.GroupLayout scalesTabLayout = new javax.swing.GroupLayout(scalesTab);
        scalesTab.setLayout(scalesTabLayout);
        scalesTabLayout.setHorizontalGroup(
            scalesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(scalesTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(noteSelectorPanelScales, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(405, Short.MAX_VALUE))
        );
        scalesTabLayout.setVerticalGroup(
            scalesTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(scalesTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(noteSelectorPanelScales, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(344, Short.MAX_VALUE))
        );

        mainTabs.addTab("Scales", scalesTab);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 842, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
        );

        mainTabs.getAccessibleContext().setAccessibleName("Main Tabs");
        mainTabs.getAccessibleContext().setAccessibleDescription("Main interface tabs.");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void delButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_delButtonMouseClicked
        removeLast();
    }//GEN-LAST:event_delButtonMouseClicked

    private void transposeUpButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_transposeUpButtonMouseClicked
        // TRANSPOSE ALL THE CHORD UP ONE SEMITONE
        for (Chord currentChord : song.getChords()) {
            currentChord.transpose(+1);
        }
        
        updateSongDisplay();
    }//GEN-LAST:event_transposeUpButtonMouseClicked

    private void transposeDownButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_transposeDownButtonMouseClicked
        // TRANSPOSE ALL THE CHORD DOWN ONE SEMITONE
        for (Chord currentChord : song.getChords()) {
            currentChord.transpose(-1);
        }

        updateSongDisplay();
    }//GEN-LAST:event_transposeDownButtonMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
          java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Kielbasa().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel chordLabel;
    private net.animats.music.app.ChordSelectorPanel chordSelectorPanel;
    private javax.swing.JPanel chordsTab;
    private javax.swing.JTextArea consoleArea;
    private javax.swing.JScrollPane consoleScrollPane;
    private javax.swing.JButton delButton;
    private javax.swing.JTabbedPane mainTabs;
    private net.animats.music.app.NoteSelectorPanel noteSelectorPanelChords;
    private net.animats.music.app.NoteSelectorPanel noteSelectorPanelScales;
    private javax.swing.JPanel scalesTab;
    private javax.swing.JLabel songLabel;
    private javax.swing.JPanel songTab;
    private javax.swing.JButton transposeDownButton;
    private javax.swing.JLabel transposeLabel;
    private javax.swing.JButton transposeUpButton;
    private javax.swing.JPanel transpositionPanel;
    // End of variables declaration//GEN-END:variables

    class GUISong extends Song implements ChordSelectorListener {

        public GUISong() {
            super("Song");
        }

        public void chordSelected(String _note, String _type) {
            Chord chord = new Chord(_note, _type);
            addChord(chord);
            delButton.setEnabled(true);
            transposeUpButton.setEnabled(true);
            transposeDownButton.setEnabled(true);

            updateSongDisplay();
        }
    }

    class IntevalSetLabel extends JLabel implements NoteSelectorListener {
        public void noteSetChanged(NoteSet _notes) {
            if (_notes.getSize() == 0) {
                setText("");
                return;
            }

            /*IntervalSet determinedIntervalSet;
            
            determinedIntervalSet = ChordType.getChordType(_notes);

            if (determinedIntervalSet == null) {
                determinedIntervalSet = ScaleType.getScaleType(_notes);
            }

            if (determinedIntervalSet == null) {
                String intervals = "Unknown (";
                for (Integer interval : _notes.getIntervalSet().getIntervals()) {
                    intervals += " " + interval;
                }
                intervals += " )";
                setText(intervals);
            } else {
                setText(_notes.getName());
            }*/
            setText (_notes.getName());
        }
    }
}
